package com.fh.modules.oms.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fh.common.web.impl.AbstractServiceImpl;
import com.fh.modules.oms.entity.OmsShoppingCar;
import com.fh.modules.oms.mapper.OmsShoppingCarMapper;
import com.fh.modules.oms.service.IOmsShoppingCarService;
import com.fh.modules.oms.vo.ShoppingCarVo;
import com.fh.modules.pms.model.mobile.CarProductModel;
import com.fh.modules.pms.model.mobile.ProductListModel;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: 购物车
 * @author： code generator
 * @date：   2019-09-28
 * @version： V1.0
 */
@Service
public class OmsShoppingCarServiceImpl extends AbstractServiceImpl<OmsShoppingCarMapper, OmsShoppingCar> implements IOmsShoppingCarService {

    @Autowired
    private OmsShoppingCarMapper omsShoppingCarMapper;

    @Override
    @Transactional
    public boolean add(ShoppingCarVo shoppingCarVo) {
        LambdaQueryWrapper<OmsShoppingCar> query=new LambdaQueryWrapper<>();
        query.eq(OmsShoppingCar::getDelFlag,0);
        query.eq(OmsShoppingCar::getMemberId,shoppingCarVo.getMemberId());
        query.eq(OmsShoppingCar::getProductId,shoppingCarVo.getProductId());
        int count=this.count(query);
        if(count>0){
            return true;
        }
        OmsShoppingCar omsShoppingCar=new OmsShoppingCar();
        BeanUtils.copyProperties(shoppingCarVo,omsShoppingCar);
        omsShoppingCar.setPropertyList(JSON.toJSONString(shoppingCarVo.getProperty()));
        return super.save(omsShoppingCar);
    }

    @Override
    public List<CarProductModel> queryMemberShoppingCar(Integer memberId, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo,pageSize,false);
        return omsShoppingCarMapper.queryMemberShoppingCar(memberId);
    }
}
